package com.webcamtracker.ui;

import com.webcamtracker.ms2.ColorHistogramIndex;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class UIQuantizedImage extends JPanel {
    private ColorHistogramIndex colorHistogramIndex;
    private BufferedImage bufferedImage;

    public void setColorHistogramIndex(ColorHistogramIndex colorHistogramIndex) {
        this.colorHistogramIndex = colorHistogramIndex;
        bufferedImage = new BufferedImage(colorHistogramIndex.width, colorHistogramIndex.height, BufferedImage.TYPE_INT_RGB);
        int width = colorHistogramIndex.width;
        int height = colorHistogramIndex.height;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int r = (colorHistogramIndex.redBinIndex[i][j] * 255) / (colorHistogramIndex.numberOfBins);
                int g = (colorHistogramIndex.greenBinIndex[i][j] * 255) / (colorHistogramIndex.numberOfBins);
                int b = (colorHistogramIndex.blueBinIndex[i][j] * 255) / (colorHistogramIndex.numberOfBins);
                bufferedImage.setRGB(j, i, new Color(r, g, b).getRGB());
            }
        }
    }

    public void paint(Graphics graphics) {
        super.paint(graphics);
        if (bufferedImage != null) {
            graphics.drawImage(bufferedImage, 0, 0, null);
        }
    }
}
